package com.yc.baseapi;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import org.apache.commons.collections4.MapUtils;
import org.omg.CORBA.INTERNAL;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class TestNumber {


	public void test2() {
		//					
		int score = (int) ((50*(2/5)) + (25*(10/35)) + (15*(20/108)) + (10*(45/709)));
		System.out.println("score = " + score);
		
		System.out.println(50*(2/5));
		System.out.println(2/5);
		
		BigDecimal d = BigDecimal.valueOf(2).divide(BigDecimal.valueOf(5), 3, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(50));//20
		BigDecimal w = BigDecimal.valueOf(10).divide(BigDecimal.valueOf(35), 3, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(25));//7.14
		BigDecimal m = BigDecimal.valueOf(20).divide(BigDecimal.valueOf(108), 3, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(15));//2.7
		BigDecimal y = BigDecimal.valueOf(45).divide(BigDecimal.valueOf(709), 3, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(10));//0.63
		score = (int) (d.add(w).add(m).add(y).doubleValue());
		System.out.println("score = " + score);
	}

	/**
	 * 四舍五入
	 */
	public void test3() {
		double num = 111231.5585;
		BigDecimal b = new BigDecimal(num);
		//保留2位小数
		double result = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		System.out.println(result);  //111231.56
	}
	
	public void testDouble() {
		double a = 2000;
		double b = 0.3111111;
		double c = a*b;
		double d = 3;
		
		double e = b + d;
		double f = c - d;
		double g = a/d;
		
		BigDecimal a1 = BigDecimal.valueOf(a);
		BigDecimal b1 = BigDecimal.valueOf(b);
		BigDecimal d1 = BigDecimal.valueOf(d);
		double c1 = a1.multiply(b1).doubleValue();//乘以
		double e1 = b1.add(d1).doubleValue();//加法
		double f1 = a1.multiply(b1).subtract(d1).doubleValue();//减法
		double g1 = a1.divide(d1, 3, BigDecimal.ROUND_HALF_UP).doubleValue();//除法  四舍五入
		
		System.out.println(c);
		System.out.println(c1);
		System.out.println(e);
		System.out.println(e1);
		System.out.println(f);
		System.out.println(f1);
		System.out.println(g);
		System.out.println(g1);
		
	}
	
	
	public void testBig() {
		int a = 9;
		BigDecimal a1 = BigDecimal.valueOf(a);
		System.out.println(a1);
		Object o = new Object();
	}

	//0 用equal比较不出来
	private void testBig2() {
		double a = 0.0;
		BigDecimal a1 = BigDecimal.valueOf(a);
		if (BigDecimal.ZERO.equals(a1.multiply(BigDecimal.valueOf(100)))) {
			System.out.println("--------");
		} else {
			System.out.println("---1-----");
		}
	}

	//0可用NumberUtil.equals比较出来
	private void testBig3() {
		double a = 0.0;
		BigDecimal a1 = BigDecimal.valueOf(a);
		if (NumberUtil.equals(BigDecimal.ZERO, a1)) {
			System.out.println("--------");
		} else {
			System.out.println("---1-----");
		}
	}
	
	/**
	 * 其实devide的函数定义如下

		BigDecimal.divide(BigDecimal divisor, int scale, RoundingMode roundingMode) ;
		
		scale为小数位数；
		
		roundingMode为小数模式；
		
		ROUND_CEILING
		如果 BigDecimal 是正的，则做 ROUND_UP 操作；如果为负，则做 ROUND_DOWN 操作。
		ROUND_DOWN
		从不在舍弃(即截断)的小数之前增加数字。
		ROUND_FLOOR
		如果 BigDecimal 为正，则作 ROUND_UP ；如果为负，则作 ROUND_DOWN 。
		ROUND_HALF_DOWN
		若舍弃部分> .5，则作 ROUND_UP；否则，作 ROUND_DOWN 。
		ROUND_HALF_EVEN
		如果舍弃部分左边的数字为奇数，则作 ROUND_HALF_UP ；如果它为偶数，则作 ROUND_HALF_DOWN 。
		ROUND_HALF_UP
		若舍弃部分>=.5，则作 ROUND_UP ；否则，作 ROUND_DOWN 。
		ROUND_UNNECESSARY
		该“伪舍入模式”实际是指明所要求的操作必须是精确的，，因此不需要舍入操作。
		ROUND_UP
		总是在非 0 舍弃小数(即截断)之前增加数字。
		写成如下便可通过
		
		BigDecimal num3 = num1.divide(num2，10，ROUND_HALF_DOWN);
	 * @param args
	 */
	
	/**
	 * 十进制 <---->  十六进制相互转换
	 */
	public void ten2Hex() {
		System.out.println(Long.toHexString(1582714660000L));//170812524a0
		System.out.println(Long.parseLong("170812524a0", 16));//1582714660000L
		
		System.out.println(Integer.toHexString(15));//f
		System.out.println(Integer.parseInt("f", 16));//15
		
		
		
	}
	
	/**
	 * 保留两位小数
	 */
	public void m2() {
		Double a = 12.0;
		Double b = 12.34;
		Double c = 12.34567;
		Double d = 0.34567;
		Double e = 12.3;
		DecimalFormat df = new DecimalFormat("#.##");
		System.out.println(df.format(a));
		System.out.println(df.format(b));
		System.out.println(df.format(c));
		System.out.println(df.format(e));
	}

	public void m3() {
		double nextOverTime = 70;
		double POLL_TIME = 100;
		int pollNum = (int)Math.ceil(nextOverTime/POLL_TIME);
		System.out.println(pollNum);
	}

	public void testIntEqual() {
		int a = 1233;
		int b = 1233;
		Integer c = 1233;
		Integer d = 1233;
		System.out.println(a==b);
		System.out.println(c==d);
		System.out.println(a==d);

	}

	public void m4() {
		try {
			new BigDecimal("12.11111");
		} catch (NumberFormatException e) {
			System.out.println("------error1");
		}
		try {
			new BigDecimal("12.1111a1");
		} catch (NumberFormatException e) {
			System.out.println("------error2");
		}

		System.out.println("-----over------");
	}

	public void m5() {
		int a = 102;
		double b = a/100;
		DecimalFormat df = new DecimalFormat("#.##");
		String c = df.format(b);
		System.out.println(c);

		Double a1 = Double.valueOf(200);
		double b1 = a1 / 100;
		String c1 = df.format(b1);
		System.out.println(c1);

		String c2 = String.format("%.2f", b1);
		System.out.println(c2);
	}

	private void getPjId(int packageId) {
		if (false) {
			System.out.println(9617);
		} else {
			System.out.println((packageId != 285192)?10524:10200);
		}
	}

	public void m8() {
		System.out.println(20%10);
	}

	public void m9() {
		double sheetNo = Math.ceil(37 / 65535);
		System.out.println(sheetNo);
	}

	public void m10() {
		double a = 1.0000;
		if (a == 1) {
			System.out.println("------------");
		}
	}

	public void m11() {
		System.out.println(org.joda.time.DateTime.now().getDayOfWeek());
		System.out.println(org.joda.time.DateTime.now().getHourOfDay());

	}

	public void m12() {
		Integer a = Convert.toInt("BDb6G");
		Integer b = Convert.toInt("4");
		Integer c = Convert.toInt(null);
		System.out.println("a=" + a + ", b=" + b + ", c=" + c);
	}

	public void m13() {
		JSONObject jo = JSONObject.parseObject("{\"FromEntId\":\"31BDb6G\"}");
		Integer fromEntId = MapUtils.getIntValue(jo, "FromEntId");
		System.out.println("fromEntId=" + fromEntId);
	}


	public static void main(String[] args) {
		TestNumber tn = new TestNumber();
//		tn.test2();
//		tn.testDouble();
//		tn.ten2Hex();
//		tn.m2();
//		tn.m3();
//		tn.testIntEqual();
//		tn.m4();
//		tn.m5();
//		tn.getPjId(285192);
//		tn.m8();
//		tn.testBig2();
//		tn.testBig3();
//		tn.m9();
//		tn.m10();
//		tn.m11();
		tn.m12();
//		tn.m13();
	}
	
}
